package hfuu.com.cn.teasystemapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hfuu.com.cn.teasystemapp.bean.land;
import hfuu.com.cn.teasystemapp.bean.operatesq;
import hfuu.com.cn.teasystemapp.bean.pick;
import hfuu.com.cn.teasystemapp.bean.tree;
import hfuu.com.cn.teasystemapp.bean.user;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import hfuu.com.cn.teasystemapp.util.SerializableMap;
import hfuu.com.cn.teasystemapp.util.listviewutil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class treeActivity extends AppCompatActivity {

    List<tree> treeList;
    List<user> userList;
    List<land> landList;
    private  int sw=0;
    private List<HashMap<String, Object>> treedata;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree);

        Intent i = getIntent();
        treeList = new ArrayList<tree>();
        landList = new ArrayList<land>();
        userList = new ArrayList<user>();
        treeList = (List<tree>) i.getSerializableExtra("treelist");
        userList = (List<user>) i.getSerializableExtra("userlist");
        landList = (List<land>) i.getSerializableExtra("landlist");

        treedata = new ArrayList<HashMap<String, Object>>();

        for (tree tree:
        treeList) {
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("treeid",tree.getTreeId());
            item.put("treevarity",tree.getTreeVarity());
            item.put("treelandname",tree.getLand().getLandName());
            item.put("treelandid",tree.getLand().getLandId());
            item.put("treeusername",tree.getUser().getUserName());
            item.put("treeuserid",tree.getUser().getUserId());
            item.put("treestate",tree.getTreeState());
            item.put("treestatename",tree.getCode().getCodeName());
            item.put("isfocus", false);
            treedata.add(item);
        }
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ListView listview = (ListView) findViewById(R.id.treelist);
        final SimpleAdapter adapter = new SimpleAdapter(this, treedata, R.layout.treelistitem, new String[]{"treevarity", "treelandname" }, new int[]{R.id.treevarity, R.id.treeland}) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                Boolean isFocus = (Boolean) treedata.get(position).get("isfocus");

                if (isFocus) {
                    view.setBackgroundColor(getResources().getColor(R.color.tv_gray));
                } else {
                    view.setBackgroundColor(Color.WHITE);
                }
                return view;
            }
        };
        listview.setAdapter(adapter);
        listviewutil.setListViewHeightBasedOnChildren(listview);

        /**
         * 添加
         */
        FloatingActionButton fb= (FloatingActionButton) findViewById(R.id.add);
        fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Log.i("landland",landList.toString());
                Intent i=new Intent();
                i.setClass(treeActivity.this,treeaddacivity.class);
                i.putExtra("landlist",(Serializable) landList);
                startActivity(i);

            }
        });


        /**
         * 修改
         */
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sw=0;
                HashMap<String, String> h1 = (HashMap<String, String>) parent.getItemAtPosition(position);
                treedata.get(position).put("isfocus", true);
                adapter.getView(position, view, parent);
                for (int i = 0; i < treedata.size(); i++) {
                    if (i != position) {
                        treedata.get(i).put("isfocus", false);
                        //  Log.i("i", "ssssssss" + i);
                        adapter.getView(i, view, parent);
                    }
                }
                adapter.getView(position, view, parent);
                adapter.notifyDataSetChanged();
                final SerializableMap serializableMap=new SerializableMap();
                Map m=new HashMap();
                m= treedata.get(position);
                serializableMap.setMap(m);

                AlertDialog.Builder dialog = new AlertDialog.Builder(treeActivity.this).setTitle("修改").setMessage("确认修改数据").setPositiveButton("是",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent=new Intent();
                                Bundle b=new Bundle();
                                b.putSerializable("map",serializableMap);
                                intent.putExtra("landlist",(Serializable) landList);
                                intent.putExtras(b);
                                intent.setClass(treeActivity.this,treeEditAcitivty.class);
                                startActivity(intent);
                            }
                        }).setNeutralButton("否", null);
                dialog.show();

            }
        });




    }
}
